<?php

class Servico {
	public $Codigo;
	public $Nome;
	public $Descricao;
	public $PrecoNormal;
	public $PrecoPromocao;
	public $PercEmpresa;
	public $PercFunc;
}

class ServicoDao {

	function Inserir($servico) {
	
		require("config.inc.php");
		
		$sql = 
			"INSERT INTO Servico (
				NOME,
				DESCRICAO,
				PRECONORMAL,
				PRECOPROMOCAO,
				PERCEMPRESA,
				PERCFUNC)
			VALUES (
				'$servico->Nome', 
				'$servico->Descricao',
				$servico->PrecoNormal,
				$servico->PrecoPromocao,
				$servico->PercEmpresa,
				$servico->PercFunc)";

		if (mysql_query($sql) == 1) {
			$servico->Codigo = mysql_insert_id();
			return 1;
		} else 
			return 0;
	}
	function Obter($codServico){
	
		require("config.inc.php");
		
		$sql = 
			"SELECT
				Codigo,
				Nome,
				Descricao,
				PrecoNormal,
				PrecoPromocao,
				PercEmpresa,
				PercFunc
			FROM Servico
			WHERE Codigo = $codServico";

		$resp = mysql_query($sql);
		
		$info = mysql_fetch_row($resp);
		
		$servico = new Servico();
		$servico->Codigo = $info[0];
		$servico->Nome = $info[1];
		$servico->Descricao = $info[2];
		$servico->PrecoNormal = $info[3];
		$servico->PrecoPromocao = $info[4];
		$servico->PercEmpresa = $info[5];
		$servico->PercFunc = $info[6];
		
		return $servico;
	}
	function Listar($nome){
		require("config.inc.php");
		$sql = 
			"SELECT
				CODIGO,
				NOME,
				DESCRICAO,
				PRECONORMAL,
				PRECOPROMOCAO,
				PERCEMPRESA,
				PERCFUNC
			FROM Servico";
			
		if(isset($nome))
			$sql = $sql." WHERE Nome LIKE %$nome%";
			
		$resp = mysql_query($sql);
		
		while($info = mysql_fetch_row($resp)) {
		
			$servico = new Servico();
			$servico->Codigo = $info[0];
			$servico->Nome = $info[1];
			$servico->Descricao = $info[2];
			$servico->PrecoNormal = $info[3];
			$servico->PrecoPromocao = $info[4];
			$servico->PercEmpresa = $info[5];
			$servico->PercFunc = $info[6];
			$arr[] = $servico;
		}
		
		return $arr;
	
	}
	function Excluir($codServico){
		require("config.inc.php");
		$sql = 
			"DELETE FROM Servico WHERE Codigo = $codServico";
			
		return mysql_query($sql);
	
	}
	function Atualizar($servico){
	
		require("config.inc.php");
		$sql = 
			"UPDATE Servico
				SET 
					NOME = '$servico->Nome',
					DESCRICAO = '$servico->Descricao',
					PRECONORMAL = $servico->PrecoNormal,
					PRECOPROMOCAO = $servico->PrecoPromocao,
					PERCEMPRESA = $servico->PercEmpresa,
					PERCFUNC = $servico->PercFunc
				WHERE
					Codigo = $servico->Codigo";
					
		return mysql_query($sql);
	}
}
?>